package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.MenmberSexAndAge;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        List<String> months = getMonthOfLastYear();
        List<Long> memberCount = new ArrayList<>();
        //统计每个月份对应的会员数量
        for (String month : months) {
            String firstDay = month + "-01"; // 2020-01-01
            String lastDay = getThisMonthLastDay(firstDay);
            long count = memberDao.findCountBeforeByDate(lastDay);
            memberCount.add(count);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;
    }

    @Override
    public MenmberSexAndAge memberSexAndAge() {
        List<Map<String, Object>> listSexfind = memberDao.findCountBymemberSex();
        List<Map<String, Object>> listSex = new ArrayList<>();
        List<Map<String, Object>> listAge = new ArrayList<>();
        String[] arrayStr = {"0-18", "18-30", "30-45", "45-60", "60以上"};

        for (Map<String, Object> map : listSexfind) {
            Map<String, Object> maptmp = new HashMap<>();
            Integer num = Integer.parseInt(map.get("sex").toString());
            if (num == 1) {
                maptmp.put("value", map.get("sexcount"));
                maptmp.put("name", "男");
                listSex.add(maptmp);
            }
            if (num == 2) {
                maptmp.put("value", map.get("sexcount"));
                maptmp.put("name", "女");
                listSex.add(maptmp);
            }
            if (num == 0) {
                maptmp.put("value", map.get("sexcount"));
                maptmp.put("name", "未填写性别信息");
                listSex.add(maptmp);
            }
        }
        for (int i = 0; i < arrayStr.length - 1; i++) {
            String[] split = arrayStr[i].split("-");
            int minage = Integer.parseInt(split[0]);
            int maxage = Integer.parseInt(split[1]);
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();
            calendar.setTime(date);
            calendar.add(Calendar.YEAR, -minage);
            Date minDate = calendar.getTime();
            calendar.setTime(date);
            calendar.add(Calendar.YEAR, -maxage);
            Date maxDate = calendar.getTime();
            Long countAge = memberDao.findCountBymemberAge(maxDate, minDate);
            Map<String, Object> maptmp = new HashMap<>();
            maptmp.put("name", arrayStr[i]);
            maptmp.put("value", countAge);
            listAge.add(maptmp);
        }

        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -60);
        Date dateMin = calendar.getTime();
        Long countAge = memberDao.findCountByAge(dateMin);
        Map<String, Object> maptmp = new HashMap<>();
        maptmp.put("name", "60以上");
        maptmp.put("value", countAge);
        Long countNull = memberDao.findCountByAgeIsNull();
        Map<String, Object> maptmp2 = new HashMap<>();
        maptmp2.put("name", "未填写性别信息");
        maptmp2.put("value", countNull);
        listAge.add(maptmp);
        listAge.add(maptmp2);
        return new MenmberSexAndAge(listAge, listSex);

    }

    @Override
    public Map<String, Object> getMemberReportChange(Date minDate, Date maxDate) {

        List<String> months = null;
        List<Long> memberCount = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String minDateStr = simpleDateFormat.format(minDate);
        String maxDateStr = simpleDateFormat.format(maxDate);
        Calendar c = Calendar.getInstance();
        c.setTime(minDate);
        c.add(Calendar.MONTH, 1);
        Date tmp = c.getTime();
        int flag = tmp.compareTo(maxDate);
        if (flag >= 0) {
            months = getMonthDay(minDate, maxDate);
        } else if ((maxDate.getTime() - minDate.getTime()) / 1000 / 60 / 60 / 24 / 365 >= 2) {
            months = getMonthYear(minDate, maxDate);
        } else{
            months = getMonth(minDate, maxDate);
        }
        for (String month : months) {
            long count = memberDao.findCountByDay(month);
            memberCount.add(count);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;

    }

    private List<String> getMonth(Date minDate, Date maxDate) {
        List<String> months = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(minDate);
        while (calendar.getTime().compareTo(maxDate) <= 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
            calendar.add(Calendar.MONTH, 1);
        }
        return months;
    }

    private List<String> getMonthYear(Date minDate, Date maxDate) {
        List<String> months = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(minDate);
        while (calendar.getTime().compareTo(maxDate) <= 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
            calendar.add(Calendar.YEAR, 1);
        }
        return months;
    }

    private List<String> getMonthDay(Date minDate, Date maxDate) {
        List<String> months = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(minDate);
        while (calendar.getTime().compareTo(maxDate) <= 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return months;
    }

    /**
     * 根据这个月第一天获取这个月最后一天
     */
    private String getThisMonthLastDay(String firstDay) {
        //2020-02-01 thisMonthFirstDay
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date firstDate = sdf.parse(firstDay);
            //创建日历对象
            Calendar cal = Calendar.getInstance();
            //设置为本月一号
            cal.setTime(firstDate);
            //调整为下月1号
            cal.add(Calendar.MONTH, 1);
            //调整为前一天(本月的最后一天)
            cal.add(Calendar.DAY_OF_MONTH, -1);
            String str = sdf.format(cal.getTime());
            return str;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取最近一年的月份
     *
     * @return
     */
    private List<String> getMonthOfLastYear() {
        List<String> months = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }
        return months;
    }
}
